import java.util.ArrayList;
import javax.swing.*;


/**
 * This class defines an HTML table element,
 * which holds one or more table-row (tr) elements, each holding
 * one or more table-data (td) elements.
 * 
 * @author Dennis Mitchell 
 * @version 1.001 09/09/2010
 */
public class HtmlTable
{
    // the rows (table-row elements) that the table holds
    private ArrayList<HtmlTableRow> rows;
    
    //default size for JFrame
    private static final int FRAME_WIDTH=500;
    private static final int FRAME_HEIGHT=500;
            
    /**
     * Constructor for the {@code HtmlTable} class. Initializes the
     * ArrayList of rows that the table holds.
     */
    public HtmlTable()
    {
        rows = new ArrayList<HtmlTableRow>();    
    }
    
    /**
     * Adds an individual row (table-row element)
     * to the table
     */    
    public void addTableRow(HtmlTableRow row)
    {
        rows.add(row);
    }
    
    /**
     * Removes all rows (table-row elements) from
     * the table
     */
    public void clearTable()
    {
        rows.clear();
    }
    
    
    /**
     * Gets the HTML for the table element, including all rows and
     * all cells and associated inline style declarations.
     * @return String holding table, tr, and td elements
     */    
    public String getHtml()
    {
        //instantiate new StringBuilder        
        StringBuilder html = new StringBuilder();
        
        //create start element for table
        html.append("<table>");
        
        //for each row in the table, append the html string
        for (HtmlTableRow r : rows)
            html.append(r.getHtml());

        //append end element for table
        html.append("</table>");
        
        //return the StringBuilder's String
        return html.toString();
        
    }
    
     /**
     *  Displays the table in a JLabel within a JFrame
     */
    public void showInJFrame()
    {
            //instantiate a new JFrame with caption for month and year
            JFrame frame = new JFrame("Table");
            
            String html = "<html><body>" + getHtml() + "</body></html>";
            
            
            //instantiate a new JLabel, whose contents are the HTML calendar,
            //and center the calendar JLabel
            JLabel htmlTable = new JLabel(html, SwingConstants.CENTER);
            
            //add the calendar JLabel to the JFrame
            frame.getContentPane().add(htmlTable);
                        
            //set the JFrame's size
            frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
                        
            //make the JFrame visible
            frame.setVisible(true);        
        
        
    }           
    
}
